<template>
  <div>
    <div class="option-box">
      <el-button type="primary" v-has="'dict:add'" @click="addDict">添加</el-button>
    </div>

    <el-table :data="tableData" stripe style="width: 100%" ref="multipleTable"
      v-loading="tableLoading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading">
      <el-table-column type="selection" />
      <el-table-column prop="name" label="名称">
        <template slot-scope="scope">
          <router-link class="dict-name" :to="{name: 'dictInfo', params: {parentId: scope.row.id}}">{{scope.row.name}}</router-link>
        </template>
      </el-table-column>
      <el-table-column prop="code" label="字典值" />
      <el-table-column prop="seq" label="排序号" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="updateTime" label="修改时间" />
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" v-has="'dict:update'" @click="editDict(scope.$index, scope.row)">编辑</el-button>
          <el-button type="text" class="text-button-del" v-has="'dict:delete'"  @click="delDict(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--dialog弹窗-->
    <el-dialog :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="35%"
      :modal-append-to-body=false
      :close-on-click-modal=false
      :close-on-press-escape=false>
      <el-form ref="dictForm" :model="dictForm" :inline=true :rules="dictFormRules" label-position="right" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="dictForm.name" placeholder="请输入名称" class="handle-input"></el-input>
        </el-form-item>
        <el-form-item label="字典值" prop="code">
          <el-input v-model="dictForm.code" placeholder="请输入字典值" class="handle-input"></el-input>
        </el-form-item>
        <el-form-item label="排序号">
          <el-input v-model="dictForm.seq" placeholder="请输入排序号" class="handle-input"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="loading"  @click="dictFormSubmit">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as dict from '@/api/dict'

export default {
  name: 'DictList',
  data () {
    return {
      dictForm: {
        id: '',
        name: '',
        code: '',
        seq: ''
      },
      dialogVisible: false, // 弹窗
      dialogTitle: '', // 弹窗标题
      isEdit: false, // 是否为修改操作
      tableData: [], // 表格数据
      tableLoading: true,
      loading: false,

      dictFormRules: {
        name: [{required: true, message: '名称不能为空', trigger: 'blur'}],
        code: [{required: true, message: '编码不能为空', trigger: 'blur'}]
      }
    }
  },
  methods: {
    queryList () {
      dict.dictList().then(res => {
        this.tableData = res.data;
        this.tableLoading = false;
      });
    },
    addDict () {
      this.isEdit = false;
      this.dialogTitle = '添加';
      this.GLOBAL.clearObject(this.dictForm);
      this.dialogVisible = true;
    },
    editDict (index, row) {
      this.isEdit = true;
      this.dialogTitle = '编辑';
      this.GLOBAL.clearObject(this.dictForm);
      dict.getById(row.id).then(res => {
        this.dictForm = res.data;
        this.dialogVisible = true;
      });
    },
    delDict (index, row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          dict.delDict(row.id)
            .then(res => {
              this.$message.success('删除成功');
              this.queryList();
            });
        })
    },
    dictFormSubmit () {
      this.$refs.dictForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          let param = this.GLOBAL.handlerQueryParam({
            ...this.dictForm
          });
          if (this.isEdit) {
            dict.editDict(param)
              .then(res => {
                this.loading = false;
                this.dialogVisible = false;
                this.queryList();
              })
              .catch(() => {
                this.loading = false;
              })
          } else {
            dict.addDict(param)
              .then(res => {
                this.loading = false;
                this.dialogVisible = false;
                this.queryList();
              })
              .catch(() => {
                this.loading = false;
              });
          }
        }
      });
    }
  },
  created () {
    this.queryList();
  },
  watch: {
    dialogVisible (val, oldValue) {
      if (val && this.$refs.dictForm !== undefined) {
        this.$refs.dictForm.resetFields();
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .dict-name{
    color: #409EFF;
    cursor: pointer;
  }
</style>
